package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.AlgorithmUtil;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.view.dialog.GoUpPopupDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 副卡补卡界面
 */
public class BuCardFragment extends BaseFragment {
    public static BuCardFragment newInstance(String cardId, String payLimit, String phone, String realName) {
        BuCardFragment buCardFragment = new BuCardFragment();
        Bundle bundle = new Bundle();
        bundle.putString("cardId", cardId);
        bundle.putString("payLimit", payLimit);
        bundle.putString("phone", phone);
        bundle.putString("realName", realName);
        buCardFragment.setArguments(bundle);
        return buCardFragment;
    }

    @BindView(R.id.tv_fu_card_name)
    TextView tvFuCardName;
    @BindView(R.id.tv_fu_card_phone)
    TextView tvFuCardPhone;
    @BindView(R.id.tv_card_yue)
    TextView tvCardYue;
    @BindView(R.id.tv_next)
    TextView tvNext;

    @Override
    protected int initLayout() {
        return R.layout.fg_bucard;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.CardReplacement), getActivity());
        tvNext.setOnClickListener(view1 -> {
            GoUpPopupDialog.newInstance(0, false, GoUpPopupDialog.REPLACEMENTCARD).show(getFragmentManager(), "dialog");
        });
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initData(Context mContext) {
        if (!StringUtils.isEmpty(getArguments().getString("payLimit"))) {
            String amounts = AlgorithmUtil.getAmount(getArguments().getString("payLimit"), 100);
            tvCardYue.setText("￥" + amounts);
        }
        tvFuCardName.setText("姓名：" + getArguments().getString("realName"));
        tvFuCardPhone.setText("电话号：" + getArguments().getString("phone"));
    }

    @Override
    protected void loadMethod() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.message) {
            case MessageEvent.REPLACEMENTCARD:
                cardReplacement(event.content);
                break;
        }
    }

    private void cardReplacement(String uuid) {

        RequestUtils.UpdateCardByUuid(getContext(), getArguments().getString("cardId"), uuid, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {
                ToastUtils.showToast(getContext(), "补卡成功");
                EventBus.getDefault().post(new MessageEvent(MessageEvent.CARD));
                pop();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
                pop();
            }
        });
    }
}
